
#include <iostream>
#include "../comm/httplib.h"
#include "oj_model.hpp"
#include "oj_control.hpp"

using namespace httplib;
using namespace ns_control;

static Control *ctrl_ptr = nullptr;

void Recover_Machine(int signo)
{
        ctrl_ptr->RecoverMachine();
}

int main()
{

        signal(SIGINT, Recover_Machine);

        Server svr;
        Control ctrl;
        ctrl_ptr = &ctrl;

        svr.Get("/all_questions", [&ctrl](const Request &req, Response &resp)
                {
                        std::string html;
                        ctrl.AllQuestions(&html);

                        resp.set_content(html, "text/html;charset=utf-8"); });

        // 用户根据题目编号，获取题目内容：/question/100（下面这里使用正则表达式）
        svr.Get(R"(/question/(\d+))", [&ctrl](const Request &req, Response &resp)
                { 
                        std::string number = req.matches[1];

                        std::string html;
                        ctrl.OneQuestion(number,&html);
                        
                        resp.set_content(html, "text/html;charset=utf-8"); });

        svr.Post(R"(/judge/(\d+))", [&ctrl](const Request &req, Response &resp)
                 {
                        std::string number = req.matches[1];
                        std::string result_json;
                        ctrl.Judge(number,req.body, &result_json);

                         resp.set_content(result_json, "application/json;charset=utf-8"); });

        svr.set_base_dir("./wwwroot");
        svr.listen("0.0.0.0", 8888);
        return 0;
}